<script setup lang='ts'>
  import { nextTick, computed, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const props = defineProps({
    gameList:{
      type:Array,
      default:()=>[]
    }
  })
  const gameList = computed<Array<any>>(()=> props.gameList)
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    list:[] as Array<any>
  })
  const listQuery = reactive({
    game_id:'',
  })
  interface formQuert {
    id:number
    pool_one_id:number | string
    man_equip_id:number | string
    girl_equip_id:number | string
    base_rate:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    pool_one_id: [{ required: true, trigger: 'blur', message: '请输入一级礼物池id' }],
    man_equip_id: [{ required: true, trigger: 'blur', message: '请输入男装备id' }],
    girl_equip_id: [{ required: true, trigger: 'blur', message: '请输入女装备id' }],
    base_rate: [{ required: true, trigger: 'blur', message: '请输入基础概率(万分)' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const { id=0 } = form
      let submitSet = false
      if(id) {
        await equipApi.pollTwoEdit(form).then(res=>{
          submitSet = true
          ToastSuccess('新增二级礼物池成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await equipApi.pollTwoAdd(form).then(res=>{
          submitSet = true
          ToastSuccess('编辑二级礼物池成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }

    })
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await equipApi.poolOneList(listQuery)
    loading.close()
    const { data=[] } = res || {}
    action.list = data
  }
  const openDialog = async(query:any = {})=> {
    const {
      id='', pool_one_id='', man_equip_id='', girl_equip_id='', base_rate=''
    } = query
    action.title = `${id?'编辑':'新增'}ID:${id}二级礼物池`
    const obj:any = {
      man_equip_id, girl_equip_id, base_rate
    }
    if(id) {
      obj['id'] = id
    }else {
      obj['pool_one_id'] = pool_one_id
    }
    Object.assign(form,obj)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <template v-if="!form.id" >
          <el-form-item :label-width="formLabelWidth" label="游戏类型">
            <el-select v-model="listQuery.game_id" @change="getList" clearable placeholder="请选择游戏类型">
              <el-option v-for="item in gameList" :label="item.game_name" :value="item.game_id" />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="一级礼物池" prop="pool_one_id">
            <el-select v-model="form.pool_one_id" clearable placeholder="请选择一级礼物池">
              <el-option v-for="item in action.list" :label="item.item_name" :value="item.id" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item :label-width="formLabelWidth" label="一级礼物池名称" prop="pool_one_id">
            <el-input v-model="form.pool_one_id" placeholder="请输入一级礼物池id" />
          </el-form-item> -->
        </template>

        <el-form-item :label-width="formLabelWidth" label="男装备id" prop="man_equip_id">
          <el-input v-model="form.man_equip_id" placeholder="请输入男装备id" />
        </el-form-item><el-form-item :label-width="formLabelWidth" label="女装备id" prop="girl_equip_id">
          <el-input v-model="form.girl_equip_id" placeholder="请输入女装备id" />
        </el-form-item><el-form-item :label-width="formLabelWidth" label="基础概率(万分)" prop="base_rate">
          <el-input v-model="form.base_rate" placeholder="请输入基础概率(万分)" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
